package project1;

import java.awt.*;
import java.io.*;
import java.util.ArrayList;

@Gomoku_Component("保存游戏并加载类")
public class SaveGomoku {
    String filePath = "save.dat";
    public void save(DrawGomoku dg) throws IOException {
        //ObjectOutputStream是Java中用于将Java对象序列化到输出流的类。它可以将对象转换为字节流，以便存储到文件或通过网络传输。
        FileOutputStream fos = new FileOutputStream(filePath);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        try {
            oos.writeObject(dg.Black_chess);
            oos.writeObject(dg.White_chess);
            oos.writeBoolean(dg.isBlackChess);
            oos.writeBoolean(dg.isGameOver);
            System.out.println("棋盘已保存到二进制文件:" + filePath);

        } finally {
            oos.close();
            fos.close();
        }
    }

    public void loadGomoku(DrawGomoku dg) throws IOException {
        FileInputStream fis = new FileInputStream(filePath);
        ObjectInputStream ois = new ObjectInputStream(fis);
        try{
            dg.Black_chess = (ArrayList<Point>) ois.readObject();
            dg.White_chess = (ArrayList<Point>) ois.readObject();
            dg.isBlackChess = ois.readBoolean();
            dg.isGameOver = ois.readBoolean();
            System.out.println("棋盘已加载成功:" + filePath);
            dg.repaint();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            ois.close();
            fis.close();
        }
    }


}
